﻿@model WebEditor.Models.Verbs
@using TextAdventures.Quest;

<div class="ui-widget-header ui-corner-all toolbar">
    <button type="button" class="verbs-add" data-key="@Model.EditorControl.Id">Add</button>
    <button type="button" class="verbs-delete">Delete</button>
</div>

@foreach (IEditorAttributeData attr in Model.EditorData.GetAttributeData().Where(a => !a.IsInherited))
{
    // Note: for now, we are excluding any inherited attributes. If we want to support these in future,
    // we will need to mark them as being inherited (e.g. display them in grey like the desktop editor),
    // and any scripts will need to copied before editing.

    object value = Model.EditorData.GetAttribute(attr.AttributeName);

    if (CanDisplayAttribute(attr.AttributeName, value))
    {
        string stringValue = value as string;
        IEditableScripts scriptValue = value as IEditableScripts;
        IEditableDictionary<IEditableScripts> dictionaryValue = value as IEditableDictionary<IEditableScripts>;

        List<SelectListItem> typesList = new List<SelectListItem>
        {
            new SelectListItem { Text = "Print a message", Value = "string", Selected = (stringValue != null) },
            new SelectListItem { Text = "Run a script", Value = "script", Selected = (scriptValue != null) },
            new SelectListItem { Text = "Require another object", Value="scriptdictionary", Selected = ( dictionaryValue != null) }
        };

        <div>
            @Html.CheckBox("verbs-select-" + attr.AttributeName, new { @class = "verbs-select" })
            @Model.Controller.GetDisplayVerbPatternForAttribute(attr.AttributeName)
            @Html.DropDownList("verbs-type-" + attr.AttributeName, typesList, new { @class = "multi-dropdown", data_key = attr.AttributeName })
            <br />
            <div class="verbChild">
            @if (scriptValue != null)
            {
                @Html.Action("EditScriptValue", new { id = Model.GameId, key = Model.Key, script = scriptValue, attribute = attr.AttributeName })
            }
            else if (stringValue != null)
            {
                @Html.TextBox(attr.AttributeName, stringValue, new { style = "width: 100%" })
            }
            else if (dictionaryValue != null)
            {
                @Html.Action("EditScriptDictionaryValue", new { id = Model.GameId, key = Model.Key, value = dictionaryValue, keyPrompt = "Please enter the object name", source = "object", attribute = attr.AttributeName })
            }
            </div>
        </div>
    }
}

@functions
{
    private static List<Type> s_validTypes = new List<Type> {
        typeof(string),
        typeof(IEditableScripts),
        typeof(IEditableDictionary<IEditableScripts>)
    };

    private bool CanDisplayAttribute(string attribute, object value)
    {
        if (!Model.Controller.IsVerbAttribute(attribute)) return false;
        Type valueType = value.GetType();
        return s_validTypes.Any(t => t.IsAssignableFrom(valueType));
    }
}